<?php

/**
 * Description of Util_UiContentProvider
 *
 * @author Young Suk Ahn
 */
class Util_ResourceHelper extends Util_Helperbase{
	
	private $resourceService;
	private $mediaService;

	public function __construct(&$request, &$requestContext)
	{
		parent::__construct($request, $requestContext);
		$this->resourceService = new Service_GenericSeedniaServiceStub(WSROOT.WSCONTEXT.'/'.SERVICE_RESOURCE.'/', ACCESS_TOKEN);
		$this->resourceService->init($requestContext);

		$this->mediaService = new Service_GenericSeedniaServiceStub(WSROOT.WSCONTEXT.'/'.SERVICE_MEDIA.'/', ACCESS_TOKEN);
		$this->mediaService->init($requestContext);
	}

	public function getResourceService()
	{
		return $this->resourceService;
	}
	
	public function getMediaServier()
	{
		return $this->mediaService;
	}

	public function getResourceByXid($xid)
	{
		return $this->resourceService->getByXid($xid);
	}
	public function addResource($record)
	{
		return $this->resourceService->add($record);
	}
	public function updateResource($record)
	{
		return $this->resourceService->update($record);
	}
	
	public function getMediaByXid($xid)
	{
		return $this->mediaService->getByXid($xid);
	}
	
	/**
	 * Adds the media record. 
	 * Pre condition: the file must have been correctly saved using saveFileResource
	 * @param type $record
	 * @return type 
	 */
	public function addMedia($record)
	{
		//$this->saveFileResource($record, $fileInputName);
		return $this->mediaService->add($record);
	}
	public function updateMedia($record)
	{
		return $this->mediaService->update($record);
	}
	
	/**
	 * Saves the resorurce physically in the respository
	 * @param type $creatorSid the user sid who created this file
	 * @param type $fileInputName the name of the input tag where the file is located
	 * @return error code
	 */
	public function saveFileResource(&$resource, $requestContext, $fileInputName = 'file', $module='resource')
	{
		if (!isset($_FILES[$fileInputName]))
			return null;
		
		$domainId = $requestContext->domainId;
		$creatorSid = $requestContext->sessionInfo->userInfo['userSid'];
		
		$resource = array('domainId' => $domainId, 'creatorSid' => $creatorSid);
		$resourceFile = $_FILES[$fileInputName];
		if ($resourceFile["error"] > 0) {
			//echo "Error: " . $resourceFile["error"] . "<br />";
			return $resourceFile["error"];
		} else {
			$resource['fileName'] = $resourceFile["name"];
			$resource['contentType'] = $resourceFile["type"];
			$resource['fileSize'] = (int)$resourceFile["size"];
			//echo "Stored in: " . $_FILES["file"]["tmp_name"];
			
			$subdir = '/resource/'; // copy under this directory
			// If it's image
			$imageInfo = getimagesize($resourceFile['tmp_name']);
			if ($imageInfo) {
				$resource['width'] =  $imageInfo[0]; 
				$resource['height'] = $imageInfo[1];
				$resource['contentType'] = $imageInfo['mime']  ;
				$subdir = '/'.$module.'/';
			}
			
			$repoPath = MEDIA_FILE_DIR.'/'.$creatorSid.$subdir;
			if (!file_exists($repoPath)) {
				mkdir($repoPath, 0700, true);
			}
			$fullPath = $repoPath.$resource['fileName'];
			$copied = copy($resourceFile['tmp_name'], $fullPath);
			$resource['repositoryUri'] = $repoPath;
		}
		
		return 0;
	}
}
